package com.h3bpm.web.controller;

import OThinker.Common.Data.BoolMatchValue;
import OThinker.Common.DateTimeUtil;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Instance.Data.InstanceData;
import OThinker.H3.Entity.Instance.InstanceState;
import OThinker.H3.Entity.Instance.PriorityType;
import OThinker.H3.Entity.Instance.Token;
import com.h3bpm.base.engine.client.PortalQuery;
import data.DataRow;
import data.DataRowCollection;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static OThinker.H3.Entity.WorkflowTemplate.WorkflowDocument.WorkflowDocument.NullWorkflowVersion;


@RequestMapping("/api/Device")
@RestController
public class ItDeviceListController extends ControllerBase {

    private static final Logger logger = LoggerFactory.getLogger(ItDeviceListController.class);


    @RequestMapping(value="/QueryIdeaIsEnd",method={RequestMethod.GET,RequestMethod.POST})
    public  Object QueryIdeaIsEnd(String workflowCode,String activityName) {
        String userID = super.getUserValidator().getUserID();
        String[] userIds = new String[1];
        userIds[0] = userID;
        InstanceState running = InstanceState.Running;
        try {
            PortalQuery portalQuery = getEngine().getPortalQuery();
            String[] dtInstance = portalQuery.GetInstanceConditions(userIds, null, null, running, workflowCode, NullWorkflowVersion, DateTimeUtil.minValue(), DateTimeUtil.maxValue(), null, PriorityType.Unspecified, BoolMatchValue.Unspecified, BoolMatchValue.Unspecified);
            System.out.println(dtInstance.length);
            DataTable dataTable = portalQuery.QueryInstance(dtInstance, 1, 100);
            DataRowCollection rows = dataTable.getRows();
            if (rows != null) {
                for (int i = 0; i < rows.size(); i++) {
                    DataRow dataRow = rows.get(i);
                    String objectId = dataRow.getString("objectId");
                    System.out.println(objectId);
                    InstanceData instanceData = new InstanceData(getEngine(), objectId, super.getUserValidator().getUserID());
                    List<Token> tokens = instanceData.getInstanceContext().getTokens();
                    List<Token> collect = tokens.stream().filter(ac -> activityName.equals(ac.getActivity())).collect(Collectors.toList());
                    if (!collect.isEmpty()){
                        Date createdTime = collect.get(0).getCreatedTime();
                        System.out.println(createdTime);
                        Date finishedTime = collect.get(0).getFinishedTime();
                        System.out.println(finishedTime);
                        System.out.println(createdTime.before(finishedTime));
                        if (createdTime != null && finishedTime != null && finishedTime.before(createdTime)) {
                            logger.info("有未完成调研数据");
                            return true;
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return "系统运行错误";
        }

        return "无未完车调研数据";
    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
